<?php

namespace app\index\controller;

use app\model\Salesman as SalesmanModel;
use think\facade\Cache;

class Salesman extends SalesmanBase
{
    public function login()
    {
        $param = request()->param(['phone', 'smsCode']);

        $code = Cache::get('smsCode');

        if ($code !== $param['smsCode'] || $code == '1234') {
            return json(['code' => 40000, 'msg' => '验证码错误']);
        }

        $user = SalesmanModel::where(['phone' => $param['phone']])->find();

        if (!$user) {
            return json(['code' => 40404, 'msg' => '用户不存在']);
        }

        return json([
            'code' => 20000,
            'msg' => '登录成功',
            'data' => [
                'token' => $user->token
            ]
        ]);
    }
}
